package com.zjh459.twitter4j;

import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.auth.RequestToken;

import com.util.Constants;

public class TwitterBasic {

	public void login(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Twitter twitter = new TwitterFactory().getInstance();
		twitter.setOAuthConsumer(Constants.CONSUMER_KEY, Constants.CONSUMER_SECRET);
		RequestToken requestToken = twitter.getOAuthRequestToken();

		String token = requestToken.getToken();
		String tokenSecret = requestToken.getTokenSecret();

		HttpSession session = request.getSession();
		session.setAttribute("token", token);
		session.setAttribute("tokenSecret", tokenSecret);

		String authUrl = requestToken.getAuthorizationURL();

		request.setAttribute("authUrl", authUrl);
//		RequestDispatcher rd = request.getRequestDispatcher("http://www.baidu.com");
		
//		rd.forward(request, response);
		response.sendRedirect("http://www.baidu.com");
	}

}
